package com.example.bootdemo.controller;

import com.alibaba.fastjson.JSON;
import com.example.bootdemo.pojo.ServiceResponse;
import com.example.bootdemo.pojo.UserInfo;
import com.example.bootdemo.pojo.UserParam;
import com.example.bootdemo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserInfoController {

    @Autowired
    private UserService userInfoService;

    @PostMapping("/info")
    public ServiceResponse<UserInfo> saveUserInfo(@RequestBody UserInfo userInfo){
       log.info("userInfo={}", JSON.toJSON(userInfo));
        ServiceResponse<UserInfo> response = new ServiceResponse<>();
        UserInfo userInfo1 = userInfoService.save(userInfo);
        response.setData(userInfo1);
        return response;
    }

    @PostMapping("/infos")
    public ServiceResponse<List<UserInfo>> saveAllUserInfos(@RequestBody List<UserInfo> userInfos){
        ServiceResponse<List<UserInfo>> response = new ServiceResponse<>();
        List<UserInfo> userInfoList = userInfoService.saveAll(userInfos);
        response.setData(userInfoList);
        return response;
    }

    @GetMapping("/info")
    public ServiceResponse<UserInfo> queryByUserName(@RequestParam("userName") String userName){
        log.info("userName={}",userName);
        ServiceResponse<UserInfo> response = new ServiceResponse<>();
        UserInfo userInfo = userInfoService.queryByUserName(userName);
        response.setData(userInfo);
        return response;
    }

    @GetMapping(value = "/findById")
    public ServiceResponse<UserInfo> queryByUserId(@RequestParam("userId") Long userId){
        log.info("userId={}",userId);
        ServiceResponse<UserInfo> response = new ServiceResponse<>();
        UserInfo userInfo = userInfoService.queryById(userId);
        response.setData(userInfo);
        return response;
    }

    @GetMapping("/findByTelNum")
    public ServiceResponse<UserInfo> findByTelNum(@RequestParam("telNum") Long telNum){
        log.info("telNum={}",telNum);
        ServiceResponse<UserInfo> response = new ServiceResponse<>();
        UserInfo userInfo = userInfoService.findByTelNum(telNum);
        response.setData(userInfo);
        return response;
    }

    @GetMapping("/findByEmail")
    public ServiceResponse<UserInfo> findByEmail(@RequestParam("email") String email){
        ServiceResponse<UserInfo> response = new ServiceResponse<>();
        log.info("email={}",email);
        UserInfo userInfo = userInfoService.findByEmail(email);
        response.setData(userInfo);
        return  response;
    }

    @GetMapping("/findByUserNameLike")
    public ServiceResponse<List<UserInfo>> findByUserNameLike(@RequestParam("userName") String userName){
        ServiceResponse<List<UserInfo>> response = new ServiceResponse<>();
        log.info("userName={}",userName);
        List<UserInfo> userInfoList = userInfoService.queryByLikeUserName(userName);
        response.setData(userInfoList);
        return response;
    }

    @GetMapping("/findByUserNameCnLike")
    public ServiceResponse<List<UserInfo>> findByUserNameCnLike(@RequestParam("userNameCn") String userNameCn){
        log.info("userNameCn={}",userNameCn);
        ServiceResponse<List<UserInfo>> response = new ServiceResponse<>();
        List<UserInfo> userInfoList=userInfoService.queryLikeByUserNameCn(userNameCn);
        response.setData(userInfoList);
        return response;
    }

    @GetMapping("findAll/orderBy")
    public ServiceResponse<List<UserInfo>> findAllOrderBy(@RequestParam("propName") String propName,String order){
        log.info("property={},order={}",propName,order);
        if(StringUtils.isEmpty(propName)){
            propName = "userId";
        }
        if(StringUtils.isEmpty(order)){
            order="ASC";
        }
        ServiceResponse<List<UserInfo>> response = new ServiceResponse<>();
        List<UserInfo> userInfoList = userInfoService.findAllOrderBy(propName,order);
        response.setData(userInfoList);
        return response;
    }

    @GetMapping("findAll/byPage/{page}/{pageSize}")
    public ServiceResponse<Page<UserInfo>> findAllByPage(@PathVariable("page") int page, @PathVariable("pageSize") int pageSize){
        if(page==0) page=1;
        if(pageSize==0) pageSize=10;
        if(pageSize>500) pageSize=500;
        log.info("page={},pageSize={}",page,pageSize);
        ServiceResponse<Page<UserInfo>> response = new ServiceResponse<>();
        Page<UserInfo> pageInfo = userInfoService.findAllByPage(page,pageSize);
        response.setData(pageInfo);
        return response;
    }

    @GetMapping("/userInfos/userRole")
    public ServiceResponse<List<UserInfo>> findByUserRole(@RequestParam("userRole") String userRole){
        log.info("userRole={}",userRole);
        ServiceResponse<List<UserInfo>> response = new ServiceResponse<>();
        List<UserInfo> userInfoList = userInfoService.findByUserRole(userRole);
        response.setData(userInfoList);
        return response;
    }

    @GetMapping("/userInfos/userName")
    public ServiceResponse<List<UserInfo>> findByLikeUserName(@RequestParam("userName") String userName){
        ServiceResponse<List<UserInfo>> response = new ServiceResponse<>();

        List<UserInfo> userInfoList = userInfoService.findByLikeUserName(userName);

        response.setData(userInfoList);

        return response;
    }

    @GetMapping("userInfo/email")
    public ServiceResponse<UserInfo> queryByEmail(@RequestParam("email") String email){

        log.info("email={}",email);

        ServiceResponse<UserInfo> response = new ServiceResponse<>();

        UserInfo userInfo = userInfoService.queryByEmail(email);

        response.setData(userInfo);

        return response;
    }

    @GetMapping("userInfos/userNameAndOrderBy")
    public ServiceResponse<List<UserInfo>> findByUserNameAndOrderBy(@RequestParam("userName") String userName, @RequestParam("orderBy") String orderBy){
        log.info("userName={},orderBy={}",userName,orderBy);

        ServiceResponse<List<UserInfo>> response = new ServiceResponse<>();

        List<UserInfo> userInfoList = userInfoService.findByUserNameOrderBy(userName,orderBy);

        response.setData(userInfoList);

        return response;
    }

    @PutMapping("/email/{username}/{email}")
    public ServiceResponse<Integer> updateUserEmailByUsername(@PathVariable("username") String username,@PathVariable("email") String email){

        ServiceResponse<Integer> response = new ServiceResponse<>();
        log.info("username={},email={}",username,email);
        Integer count = userInfoService.updateEmailByUsername(username,email);
        response.setData(count);
        return response;
    }

    @GetMapping("/example/{userName}")
    public ServiceResponse<UserInfo> findOneByExampleUserName(@PathVariable("userName") String userName){
        ServiceResponse<UserInfo> response = new ServiceResponse<>();
        log.info("userName={}",userName);
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName(userName);
        UserInfo data = userInfoService.findOneByExample(userInfo);
        response.setData(data);
        return response;
    }

    @GetMapping("/example/list")
    public ServiceResponse<List<UserInfo>> findListByExample(@RequestParam("userName") String userName,@RequestParam("userRole") String userRole){

        ServiceResponse<List<UserInfo>> response = new ServiceResponse<>();
        log.info("userName={},userRole={}",userName,userRole);
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName(userName);
        userInfo.setUserRole(userRole);
        List<UserInfo> data = userInfoService.findAllByExample(userInfo);
        response.setData(data);
        return response;
    }

    @PostMapping("/list/conditions")
    public ServiceResponse<List<UserInfo>> findUsersByConditions(@RequestBody UserParam userParam){

        log.info("userParam={}",JSON.toJSON(userParam));

        ServiceResponse<List<UserInfo>> response = new ServiceResponse<>();

        List<UserInfo> data = userInfoService.findAllByDynamicConditions(userParam);

        response.setData(data);

        return response;
    }


}
